﻿@model SPagedList<Dyd.BaseService.ServiceCenter.Domain.Model.tb_node>
@{
    ViewBag.Title = "服务节点";
    Layout = "~/Views/Shared/_LayoutPage.cshtml";
    var list = ViewBag.ServiceList as IList<SelectListItem>;
    list.Insert(0, new SelectListItem { Value = "0", Text = "全部" });
}
@Html.AntiForgeryToken()
<div class="main_content">
    <div class="top">
        <h3>@ViewBag.Title</h3>
    </div>
    <div class="main">
        @Html.AntiForgeryToken()
        @using (Ajax.BeginForm("NodeIndex", "Node", null, new AjaxOptions { UpdateTargetId = "ajaxList", InsertionMode = InsertionMode.Replace }, new { id = "ajaxForm" }))
        {
            <div class="pt10">
                <label>服务</label>
                @Html.DropDownList("serviceId", list)
                <label>ip地址</label>
                <input value="" type="text" name="ip" id="ip" style="width:150px;" maxlength="15" />
                <input type="submit" class="btn" value="搜索" accesskey="S" />
            </div>
        }
    </div>

    <div id="ajaxList" class="m10">
        @Html.Partial("_NodeIndex", Model)
    </div>
    <div class="layer_notice"></div>
</div>
@section Scripts{
    <script src="~/Scripts/layer/layer.js"></script>
    <link href="~/Scripts/layer/skin/layer.css" rel="stylesheet" />
    <script src="~/Scripts/jsonformat.js"></script>
}

@section footerScripts{
    @Html.SPagerScript()
    <script>
        $(function () {

            $("#ajaxList").on("click", function (event) {
                var type = $(event.target).attr("data-operate");
                switch (type) {
                    case "setDefaultProtocol": setDefaultProtocol(event.target); break;
                    case "showProtocolJson": showProtocolJson(event.target); break;
                    case "showReport": showReport(event.target); break;
                    case "restartNode": restartNode(event.target); break;
                    case "deleteItem": deleteItem(event.target); break;
                }                  
            });
        });
        

        //重启节点
        function restartNode(obj) {
            var id = $(obj).parent().parent().attr("data-id");
            if (confirm("确认设置")) {
                $.ajax({
                    url: "/Node/RestartNode",
                    type: "post",
                    data: antiToken({ id: id }),
                    dataType: "json",
                    success: function (data) {
                        if (data.Flag) {
                            alert(data.Message);
                            window.location.reload();
                        } else {
                            alert(data.Message);
                        }
                    }
                });
            }
        }

        //设置为默认协议
        function setDefaultProtocol(obj) {
            var id = $(obj).parent().parent().attr("data-id");
            if (confirm("确认设置")) {
                $.ajax({
                    url: "/ProtocolVersion/SetDefaultProtocolVersion",
                    type: "post",
                    data: antiToken({ id: id }),
                    dataType: "json",
                    success: function (data) {
                        if (data.Flag) {
                            alert(data.Message);
                            window.location.reload();
                        } else {
                            alert(data.Message);
                        }
                    }
                });
            }
        }

        function showProtocolJson(obj) {
            var id = $(obj).parent().parent().attr("data-id");
            var url = "/Node/NodeProtocalJson?id=" + id;
            window.open(url)
            //var protocalJson = $(obj).attr("data-title");
            //JsonUti.n = "<br />";
            //JsonUti.t = "&nbsp;&nbsp;&nbsp;";
            //protocalJson = JsonUti.convertToString(eval("(" + protocalJson + ")"));
            //layer.open({
            //    type: 0,
            //    title: false,
            //    scrollbar: true,
            //    closeBtn: true,
            //    shadeClose: true,
            //    skin: 'layui-layer-iframe',
            //    area: ['80%', '90%'],
            //    content: protocalJson
            //});
        }


        //禁用启用
        function showReport(obj) {
            var id = $(obj).parent().parent().attr("data-id");
            var url = "/NodeReport/NodeReportIndex?nodeid=" + id;
            window.open(url);
        }

        //删除
        function deleteItem(obj) {
            var id = $(obj).parent().parent().attr("data-id");
            if (confirm("确认删除")) {
                $.ajax({
                    url: "/Node/NodeIndexDelete",
                    type: "post",
                    data: antiToken({ id: id }),
                    dataType: "json",
                    success: function (data) {
                        if (data.Flag) {
                            alert(data.Message);
                            window.location.reload();
                        } else {
                            alert(data.Message);
                        }
                    }
                });
            }
        }

        function changeBoostPercent(id) {

            var ret = true;
            var boostpercent = $("#boostpercent" + id).val();
            if (ret) {
                $.ajax({
                    url: '/Node/UpdateNodeBoostPercent',
                    type: "post",
                    data: {
                        boostpercent: boostpercent,
                        id: id
                    },
                    success: function (data) {
                    },
                    error: function () {
                        alert("你的网络开小差了，请重试。");
                    }
                });
            }
        }
    </script>

}